<?php
function UserNameAvailavility($Username)
{
	$DbObject= new sql_db(HOSTNAME, USERNAME, PASSWORD, DATABASE, true);
	$DbObject->db_connect_id;
	$SELECTusername="SELECT `username` FROM ".SITEUSER." WHERE `username`='".$Username."'";
	$EXECUTIONofSELECTusername=$DbObject->sql_query($SELECTusername);
	if($DbObject->sql_numrows($EXECUTIONofSELECTusername) > 0)
	{
		return 'Y';
	}
	else
	{
		return 'N';
	}
	
}

function UserEmailAvailavility($EMAil)
{
	$DbObject= new sql_db(HOSTNAME, USERNAME, PASSWORD, DATABASE, true);
	$DbObject->db_connect_id;
	$SELECTusername="SELECT `email` FROM ".SITEUSER." WHERE `email`='".$EMAil."'";
	$EXECUTIONofSELECTusername=$DbObject->sql_query($SELECTusername);
	if($DbObject->sql_numrows($EXECUTIONofSELECTusername) > 0)
	{
		return 'Y';
	}
	else
	{
		return 'N';
	}
	
}


function DisplayNameAvailavility($DIsplaYNAMe)
{
	$DbObject= new sql_db(HOSTNAME, USERNAME, PASSWORD, DATABASE, true);
	$DbObject->db_connect_id;
	$SELECTusername="SELECT `publishName` FROM ".USERMASTER." WHERE `publishName`='".$DIsplaYNAMe."'";
	$EXECUTIONofSELECTusername=$DbObject->sql_query($SELECTusername);
	if($DbObject->sql_numrows($EXECUTIONofSELECTusername) > 0)
	{
		return 'Y';
	}
	else
	{
		return 'N';
	}
	
}

function CheckLogin()
{
	$PagefunctionObject=new siteFunctions();
	$SESSION=new Session();
	$DbObject= new sql_db(HOSTNAME, USERNAME, PASSWORD, DATABASE, true);
	$DbObject->db_connect_id;
	$GETSEssionUSERid=$PagefunctionObject->SESSIONVALUE('AdMobUserId');
	//echo $GETSEssionUSERid.'<br>';
	$SELECTISuser="SELECT `siteuserId` FROM ".SITEUSER." WHERE `siteuserId`='".$GETSEssionUSERid."'";
	//echo $SELECTISuser;die();
	$EXECUTIONofSELECTISuser=$DbObject->sql_query($SELECTISuser);
	if($DbObject->sql_numrows($EXECUTIONofSELECTISuser) > 0)
	{
		$SESSION->set('ISuser', "Yes");
		//echo $PagefunctionObject->SESSIONVALUE('ISuser');
		//die();
		$GETSESSIOnvalueUSER=$PagefunctionObject->SESSIONVALUE('ISuser');
		return $GETSESSIOnvalueUSER;		
	}
	else
	{
		
		return false;
	}
	if($GETSEssionUSERid != '')
	{
		$ISPICKer=CheckLoginIsPicker($GETSEssionUSERid);
		$ISPAIDUser=CheckLoginIsUser($GETSEssionUSERid);
		if($ISPICKer == 'N' && $ISPAIDUser == 'N')
		{
			$SESSION->destroy();
			$PagefunctionObject->RedirectPageTo("Pagelogin");
		}
		
	}
	else
	{
		$SESSION->destroy();
		$PagefunctionObject->RedirectPageTo("Pagelogin");
	}
	
	
}




function CheckLogi__1()
{
	$PagefunctionObject=new siteFunctions();
	$SESSION=new Session();
	$GETSEssionUSERid=$PagefunctionObject->SESSIONVALUE('AdMobUserId');	
	if($GETSEssionUSERid != '')
	{
		$ISPICKer=CheckLoginIsPicker($GETSEssionUSERid);
		$ISPAIDUser=CheckLoginIsUser($GETSEssionUSERid);
		if($ISPICKer == 'N' && $ISPAIDUser == 'N')
		{
			$SESSION->destroy();
			$PagefunctionObject->RedirectPageTo("Pagelogin");
		}
		
	}
	else
	{
		$SESSION->destroy();
		$PagefunctionObject->RedirectPageTo("Pagelogin");
	}
	
	
}

function CheckLoginIsPicker($USERid)
{
	$DbObject= new sql_db(HOSTNAME, USERNAME, PASSWORD, DATABASE, true);
	$DbObject->db_connect_id;
	$SESSION=new Session();
	$PagefunctionObject=new siteFunctions();
	$SELECTISpicker="SELECT `isPicker` FROM ".SITEUSER." WHERE `siteuserId`='".$USERid."'";
	$EXECUTIONofSELECTISpicker=$DbObject->sql_query($SELECTISpicker);
	if($DbObject->sql_numrows($EXECUTIONofSELECTISpicker) > 0)
	{
		$RESULTofEXECUTIONofSELECTISpicker=$DbObject->sql_fetchrow($EXECUTIONofSELECTISpicker);
		$SESSION->set('AdMobISpicker', $RESULTofEXECUTIONofSELECTISpicker['isPicker']);
		$GETSESSIOnvalue=$PagefunctionObject->SESSIONVALUE('AdMobISpicker');
		return $GETSESSIOnvalue;
	}
	else
	{
		$SESSION->varunset('AdMobISpicker');
		return 'N';
	}
	
}

function CheckLoginIsUser($USERid)
{
	$DbObject= new sql_db(HOSTNAME, USERNAME, PASSWORD, DATABASE, true);
	$DbObject->db_connect_id;
	$SESSION=new Session();
	$PagefunctionObject=new siteFunctions();
	$SELECTISuser="SELECT `isUser` FROM ".SITEUSER." WHERE `siteuserId`='".$USERid."'";
	$EXECUTIONofSELECTISuser=$DbObject->sql_query($SELECTISuser);
	if($DbObject->sql_numrows($EXECUTIONofSELECTISuser) > 0)
	{
		$RESULTofEXECUTIONofSELECTISuser=$DbObject->sql_fetchrow($EXECUTIONofSELECTISuser);
		$SESSION->set('AdMobISuser', $RESULTofEXECUTIONofSELECTISuser['isUser']);
		$GETSESSIOnvalueUSER=$PagefunctionObject->SESSIONVALUE('AdMobISuser');
		return $GETSESSIOnvalueUSER;
		
	}
	else
	{
		$SESSION->varunset('AdMobISuser');
		return 'N';
	}
	
}

function CREATEdatabaseOBJECT()
{
	return new sql_db(HOSTNAME, USERNAME, PASSWORD, DATABASE, true); 
}

function CREATEsessionOBJECT()
{
	return new Session();; 
}

function GETlastWEEK()
{
	$DAY_OF_WEEK=date('w');
	$DIFFErance=(int)((($DAY_OF_WEEK - (int)1) + 3) % 7);
	return $DIFFErance;
}

function GETlastMONTH()
{
	return date("Y-m",mktime(0,0,0,(date('m') - 1),1,date("Y")));
}

function GETlastYEAR()
{
	return date("Y",mktime(0,0,0,date('m'),1,(date("Y") - 1)));
}


function num_weeks_in_year($year) 
{
	return date('W', mktime(0,0,0,12,28,$year) );
}

/*function num_weeks_in_year($year) {
	return date('W', mktime(0,0,0,12,28,$year) );
}*/


function getDaysInWeek ($weekNumber, $year,$getPosition='') {
  // Count from '0104' because January 4th is always in week 1
  // (according to ISO 8601).
  $time = strtotime($year . '0104 +' . ($weekNumber - 1). ' weeks');
  // Get the time of the first day of the week
  $mondayTime = strtotime('-' . (date('w', $time) - 1) . ' days',$time);
  // Get the times of days 0 -> 6
  $dayTimes = array ();
  for ($i = 0; $i < 7; ++$i) {
    $dayTimes[] = strtotime('+' . $i . ' days', $mondayTime);
  }
  // Return timestamps for mon-sun.
  if($getPosition != '')
  {
  	return $dayTimes[$getPosition];
  }
  else
  	return $dayTimes;
}

//A simple way to test if it works:

/*$dayTimes = getDaysInWeek(33, 2009);

#print_r($dayTimes);
foreach ($dayTimes as $dayTime) {
  #echo (strftime('%a - %Y-%m-%d', $dayTime) . "\n").'<br>';
}*/

function getfirstDateOfFirstWeekInMonth ($monthNumber, $year,$getPosition='') {
  // Count from '0104' because January 4th is always in week 1
  // (according to ISO 8601).
  $time = strtotime($year . '0104 +' . ($monthNumber - 1). ' months');
  // Get the time of the first day of the week
  $FirstDayTime = strtotime('-' . (date('m', $time) - 1) . ' months',$time);
  // Get the times of days 0 -> 6
  $dayTimes = array ();
  for ($i = 0; $i < 31; ++$i) {
    $dayTimes[] = strtotime('+' . $i . ' days', $FirstDayTime);
  }
  // Return timestamps for mon-sun.
  if($getPosition != '')
  {
  	return $dayTimes[$getPosition];
  }
  else
  	return $dayTimes;
}
/*$dayTimes = getfirstDateOfFirstWeekInMonth(1, 2009);

print_r($dayTimes);
foreach ($dayTimes as $dayTime) {
  echo (strftime('%a - %Y-%m-%d', $dayTime) . "\n").'<br>';
}
*/

?>
